const express = require('express');
const router = express.Router();
const sql = require('../db.js');

// 获取首页商品
router.get('/index', function(req, res, next) {
    sql('select id,title,img_url,new_price from index_shop', [])
        .then((result) => {
            res.json({ code: 200, data: result })
        }).catch((err) => {
        console.log(err)
        res.json({ code: 500, data: '程序错误' })
    })
});

// 查询每个商品
router.get('/shop/:id', (req, res) => {
    if (!isNaN(req.params.id)) {
        sql('select * from index_shop where id=?', req.params.id)
            .then((result) => {
                res.json({ code: 200, data: result })
            })
            .catch(() => {
                res.json({ code: 500, data: '程序错误' })
            })
    } else {
        res.json({ code: 400, data: '参数错误' })
    }
})

router.get('/gou/shop/:id', (req, res) => {
  if (!isNaN(req.params.id)) {
      sql('select * from gou where id=?', req.params.id)
          .then((result) => {
              res.json({ code: 200, data: result })
          })
          .catch(() => {
              res.json({ code: 500, data: '程序错误' })
          })
  } else {
      res.json({ code: 400, data: '参数错误' })
  }
})

// 获取购物车商品
router.get('/shopCar/cngg/ggg/main', function(req, res, next) {
  sql('select id,title,img_url,new_price from gou', [])
      .then((result) => {
          res.json({ code: 200, data: result })
      }).catch((err) => {
      console.log(err)
      res.json({ code: 500, data: '程序错误' })
  })
});

// 用户注册
router.post('/register', (req, res) => {
    if (req.body) {
        sql('insert into users (uname, phonenum, imgbase) values (?, ?, ?)',
            [req.body.uname, req.body.phonenum, req.body.imgbase])
            .then((result) => {
                if (result.affectedRows > 0) {
                    res.json({ code: 200, data: '新增成功' })
                } else {
                    res.json({ code: 400, data: '新增失败' })
                }
            })
            .catch(() => {
                res.json({ code: 500, data: '程序错误' })
            })
    }
})

// 用户登录
router.post('/sign', (req, res) => {
    if (req.body) {
        sql('select * from users where uname=? and phonenum=?',
            [req.body.uname, req.body.phonenum])
            .then((result) => {
                res.json({ code: 200, data: result })
            })
            .catch(() => {
                res.json({ code: 500, data: '程序错误' })
            })
    }
})

// 添加地址
router.post('/add_address', (req, res) => {
    if (req.body) {
        sql('insert into usr_address (id, name, phone, address) values (?, ?, ?, ?)',
            [req.body.id, req.body.name, req.body.phone, req.body.address])
            .then((result) => {
                if (result.affectedRows > 0) {
                    res.json({ code: 200, data: '新增成功' })
                } else {
                    res.json({ code: 400, data: '新增失败' })
                }
            })
            .catch(() => {
                res.json({ code: 500, data: '程序错误' })
            })
    }
})
// 地址展示
router.get('/address/:id', (req, res) => {
    if (req.params.id) {
        sql('select * from usr_address where id=?', req.params.id)
            .then((result) => {
                res.json({ code: 200, data: result })
            })
            .catch(() => {
                res.json({ code: 500, data: '程序错误' })
            })
    } else {
        res.json({ code: 400, data: '参数错误' })
    }
  })
router.get('/get_find/:id', (req, res) => {
    if (req.params.id) {
        sql('select * from usr_address where add_id=?', req.params.id)
            .then((result) => {
                res.json({ code: 200, data: result })
            })
            .catch(() => {
                res.json({ code: 500, data: '程序错误' })
            })
    } else {
        res.json({ code: 400, data: '参数错误' })
    }
})
// 修改地址
router.put('/update_find', (req, res) => {
    if (req.body) {
        sql('update usr_address set name=?, phone=?, address=? where add_id=?',
            [req.body.name, req.body.phone, req.body.address, req.body.add_id])
            .then((result) => {
                res.json({ code: 200, data: result })
            })
            .catch(() => {
                res.json({ code: 500, data: '程序错误' })
            })
    } else {
        res.json({ code: 400, data: '参数错误' })
    }
})
// 删除地址
router.delete('/del_add/:id', (req, res) => {
    if (req.params.id) {
        sql('delete from usr_address where add_id=?', req.params.id)
            .then((result) => {
                if (result.affectedRows>0) {
                    res.json({ code: 200, data: '删除成功' })
                } else {
                    res.json({ code: 400, data: '删除失败' })
                }
            })
            .catch(() => {
                res.json({ code: 500, data: '程序错误' })
            })
    } else {
        res.json({ code: 400, data: '参数错误' })
    }
})

// 添加亲友
router.post('/add_friends', (req, res) => {
    if (req.body) {
        sql('insert into friends (phonenum, f_num) values (?, ?)',
            [req.body.phonenum, req.body.f_num])
            .then((result) => {
                if (result.affectedRows > 0) {
                    res.json({ code: 200, data: '新增成功' })
                } else {
                    res.json({ code: 400, data: '新增失败' })
                }
            })
            .catch(() => {
                res.json({ code: 500, data: '程序错误' })
            })
    }
})
// 亲友展示
router.get('/getfriends/:id', (req, res) => {
    if (req.params.id) {
        sql('select * from users, (select f_num from friends where phonenum=?)f where phonenum=f.f_num', req.params.id)
            .then((result) => {
                res.json({ code: 200, data: result })
            })
            .catch(() => {
                res.json({ code: 500, data: '程序错误' })
            })
    } else {
        res.json({ code: 400, data: '参数错误' })
    }
})

// 寄件
router.post('/sendexp', (req, res) => {
    if (req.body) {
        sql('insert into express (j_num, j_name, s_num, s_name, info) values (?, ?, ?, ?, ?)',
            [req.body.j_num, req.body.j_name, req.body.s_num, req.body.s_name, req.body.info])
            .then((result) => {
                if (result.affectedRows > 0) {
                    res.json({ code: 200, data: '新增成功' })
                } else {
                    res.json({ code: 400, data: '新增失败' })
                }
            })
            .catch(() => {
                res.json({ code: 500, data: '程序错误' })
            })
    }
})
// 获取寄件和收件信息
router.get('/getexp/:id', (req, res) => {
    if (req.params.id) {
        sql('SELECT * FROM express WHERE j_num=? OR s_num=?',
            [req.params.id, req.params.id])
            .then((result) => {
                res.json({ code: 200, data: result })
            })
            .catch(() => {
                res.json({ code: 500, data: '程序错误' })
            })
    } else {
        res.json({ code: 400, data: '参数错误' })
    }
})
module.exports = router;